package classes
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.BitmapAsset;
	import mx.utils.ObjectProxy;
			
	
	public class OLD_ImageLoader
	{
		private var original:BitmapData;
		private var a :PhotoObject = new PhotoObject();
		public var photoImage:ObjectProxy;
		private var originalFile:File;	
		private var imageLoader:Loader;
		private static const MAX_WIDTH:uint = 2880;
		private static var MAX_HEIGHT:uint = 2880;
		
		public function OLD_ImageLoader()
		{
			photoImage = new ObjectProxy(a);
		}
		public function loadImages(files:FileListEvent):void
		{
			trace("LOADING FILES");
			for each (var file:File in files.files)
			{
				trace("LOAD IMAGE: "+file.name);
				loadImage(file);				
			}					
		}				
		public function loadImage(file:File):void
		{								
				trace("LOAD IMAGE FILE");
				originalFile = file;
			 	var request:URLRequest = new URLRequest(file.url);		 
			    imageLoader = new Loader();
			 	imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageCompleteHandler);
			 	imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageErrorHandler);			 	
			 	// add other listeners here
			 	imageLoader.load(request)		 			 	
		}
		private function imageCompleteHandler(event:Event):void
		{
			try{		
				trace(" ### imageComppleteHandler ###");
			 	var bitmapData:BitmapData = Bitmap(event.currentTarget.content).bitmapData;
			 				
			 	var originalWidth:Number = bitmapData.width;
			 	var originalHeight:Number = bitmapData.height;
			 	var newWidth:Number = originalWidth;
			 	var newHeight:Number = originalHeight;
			 	var bitmapAsset : BitmapAsset;		
			 	var m:Matrix = new Matrix();
			 			
			 	var scaleX:Number = 1;
			 	var scaleY:Number = 1;
			 
			 	if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT){
			 		trace(" << Image Resized >> ");
			  		var sx:uint =  MAX_WIDTH / originalWidth;
			  		var sy:uint = MAX_HEIGHT / originalHeight;
			  		var scale:Number = Math.min(sx, sy);
			  		newWidth = originalWidth * scale;
			  		newHeight = originalHeight * scale;	
			  	}
			 	m.scale(scale, scale);
			 	trace(" << Matrix Scaled >> ");
			 	
			 	original = new BitmapData(newWidth, newHeight); 		 				
			 	original.draw(bitmapData, m);
			 
			 	trace(" << New BitmapData (re-scaled) >> ");
			 		 	
			 	photoImage.title = originalFile.name;
			 	photoImage.bitmapData = original;
			 	photoImage.url = originalFile.nativePath;
			 	photoImage.transforms = m;
			 	photoImage.imageObject = new Image();
			 	photoImage.imageObject.source = new BitmapAsset(photoImage.bitmapData);
			 	trace(" << photoImage bitmapData  "+photoImage.title+">> ");
			 			
				trace(" << thumbnailsList Updated: >> \n"
							+originalFile.url+"\n"
							
							+photoImage.title+"\n"
							+photoImage.imageObject+ " ");													
			}
			catch (e:ArgumentError) {
				trace ("error?= "+e);
			}	
			
		}
		
		private function imageErrorHandler(event:IOErrorEvent):void
		{
			trace("Fout");
			 Alert.show(event.text,"Could not load images.");
		}		
	}
}